<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>随机生成指定长度的字符串</title>
</head>

<body>
    <script type="text/javascript">
        function randomString(n) {
            var str = getLetters();
            var tmp = '',
                i = 0;
            for (i = 0; i < n; i++) {
                tmp = tmp + str.charAt(Math.floor(Math.random() * str.length));
            }
            return tmp;
        }
        console.log(randomString(3));

        function getLetters() {
            var str = '';
            for (var i = 0; i < 26; i++) {
                str = str + String.fromCharCode(97 + i);
            }
            return str;
        }
    </script>
</body>

</html>